package pwd.allen.plugin;

import lombok.Data;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author pwdan
 * @create 2022-09-08 9:56
 **/
@Data
public class ShardDateParam implements Serializable {
    /**
     * 分表格式
     */
    private SimpleDateFormat simpleDateFormat;
    /**
     * 分表表名
     */
    private String tableName;
    /**
     * 开始时间
     */
    private Date beginDate;
    /**
     * 结束时间
     */
    private Date endDate;
    /**
     * 新sql
     */
    private String newSql;

    /**
     * 是否执行完prepare拦截方法后从ThreadLocal中移除
     */
    private boolean ifRemoveAfterPrepare = true;

    /**
     * 如果有多个ShardDateParam，用链表形势关联下个ShardDateParam
     */
    private ShardDateParam next;

    /**
     * 分表配置
     */
    private ShardDateInfo shardDateInfo;

    /**
     * 分表日期
     */
    private List<String> dateStrs;

    public ShardDateParam(SimpleDateFormat simpleDateFormat, String tableName, Date beginDate, Date endDate) {
        this.simpleDateFormat = simpleDateFormat;
        this.tableName = tableName;
        this.beginDate = beginDate;
        this.endDate = endDate;
    }

    public ShardDateParam deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(this);//需要支持序列化哦

        ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        return (ShardDateParam) objectInputStream.readObject();
    }
}
